﻿using CommunityToolkit.Mvvm.ComponentModel;
using LiveCharts;
using LiveCharts.Wpf;
using System.Collections.ObjectModel;
using System.Windows.Media;

namespace KxApp.Chart
{
    public partial class ChartService : ObservableObject
    {
        public ChartService()
        {
            InitializeLineData();
            InitializePieData();
            InitializeBarData();
            InitializeBarData1();
        }

        #region 折线图
        [ObservableProperty]
        private SeriesCollection lineSeries = new();

        [ObservableProperty]
        private ObservableCollection<string> xAxisLabels = new();

        public LineSeries Series1 { get; set; } = new();
        public LineSeries Series2 { get; set; } = new();
        public LineSeries Series3 { get; set; } = new();

        public double LineValue1 { get; set; }
        public double LineValue2 { get; set; }
        public double LineValue3 { get; set; }

        private void InitializeLineData()
        {
            Series1 = new LineSeries
            {
                Title = $"焊接电压(V)",
                Values = new ChartValues<double>(),
                //Stroke = initialColors[i],
                Fill = null,
                //Foreground = Brushes.Red,
                StrokeThickness = 3,
            };

            Series2 = new LineSeries
            {
                Title = $"焊接电流(A)",
                Values = new ChartValues<double>(),
                //Stroke = initialColors[i],
                Fill = null,
                //Foreground = Brushes.Red,
                StrokeThickness = 3,
            };
            Series3 = new LineSeries
            {
                Title = $"送丝速度(m/min)",
                Values = new ChartValues<double>(),
                //Stroke = initialColors[i],
                Fill = null,
                //Foreground = Brushes.Red,
                StrokeThickness = 3,
            };

            LineSeries.Add(Series1);
            LineSeries.Add(Series2);
            LineSeries.Add(Series3);
        }

        public void UpdateLineData()
        {
            var series0 = LineSeries[0] as LineSeries;
            var series1 = LineSeries[1] as LineSeries;
            var series2 = LineSeries[2] as LineSeries;
            //series0.Foreground = Brushes.Red;
            //Math.Round(LineValue1, 2);
            double line1, line2, line3;
            line1 = LineValue1;
            line2 = LineValue2;
            line3 = LineValue3;

            //if (LineValue1<0.2)
            //{
            //    line1 = 0.1;
            //}
            //if (LineValue2 < 0.2)
            //{
            //    line2 = 0.1;
            //}
            //if (LineValue2 < 0.2)
            //{
            //    line3 = 0.1;
            //}
            series0.Values.Add(Math.Round(line1, 2));
            series1.Values.Add(Math.Round(line2, 2));
            series2.Values.Add(Math.Round(line3, 2));
            XAxisLabels.Add(DateTime.Now.ToString("HH:mm:ss"));

            if (series0.Values.Count > 10)
            {
                series0.Values.RemoveAt(0);
            }
            if (series1.Values.Count > 10)
            {
                series1.Values.RemoveAt(0);
            }
            if (series2.Values.Count > 10)
            {
                series2.Values.RemoveAt(0);
            }

            if (XAxisLabels.Count > 10)
            {
                XAxisLabels.RemoveAt(0);
            }
        }
        #endregion

        #region 饼图
        private void InitializePieData()
        {
            // 初始化饼图数据
            //var initialValues = new List<double> { A, B, C };
            //var initialColors = new List<Brush> { Brushes.Purple, Brushes.Red, Brushes.Blue };

            var initialValues = new List<double> { PieValue1, PieValue2, };

            var series0 = new PieSeries
            {
                Title = $"当前产品合格率(%)",
                Values = new ChartValues<double> { initialValues[0] },
                LabelPoint = (a) =>
                {
                    return $"合格率{PieValue1}%";
                },
                DataLabels = true,
                FontSize = 14
                //Fill = initialColors[i]
            };

            var series1 = new PieSeries
            {
                Title = $"当前产品不合格率(%)",
                Values = new ChartValues<double> { initialValues[1] },
                LabelPoint = (a) =>
                {
                    return $"不合格率{PieValue2}%";
                },
                DataLabels = true,
                FontSize = 14
                //Fill = initialColors[i]
            };

            //var series2 = new PieSeries
            //{
            //    Title = $"框架件产量",
            //    Values = new ChartValues<double> { initialValues[2] },
            //    //Fill = initialColors[i]
            //};

            //var series3 = new PieSeries
            //{
            //    Title = $"大底座产量",
            //    Values = new ChartValues<double> { initialValues[3] },
            //    //Fill = initialColors[i]
            //};

            //var series4 = new PieSeries
            //{
            //    Title = $"小底座产量",
            //    Values = new ChartValues<double> { initialValues[4] },
            //    //Fill = initialColors[i]
            //};

            PieValues.Add(series0);
            PieValues.Add(series1);
            //PieValues.Add(series2);
            //PieValues.Add(series3);
            //PieValues.Add(series4);
        }

        [ObservableProperty]
        private SeriesCollection pieValues = new();

        public double PieValue1 { get; set; }
        public double PieValue2 { get; set; }
        public double PieValue3 { get; set; }
        public double PieValue4 { get; set; }
        public double PieValue5 { get; set; }

        public void UpdatePieData()
        {
            var series0 = PieValues[0] as PieSeries;
            var series1 = PieValues[1] as PieSeries;
            //var series2 = PieValues[2] as PieSeries;
            //var series3 = PieValues[3] as PieSeries;
            //var series4 = PieValues[4] as PieSeries;
            //var series2 = PieValues[2] as PieSeries;
            series0.Values[0] = PieValue1;
            series1.Values[0] = PieValue2;
            //series0.LabelPoint = (a) =>
            //{
            //    return $"焊丝消耗{PieValue1}m";
            //};

            //series1.LabelPoint = (a) =>
            //{
            //    return $"气体消耗{PieValue2}L";
            //};

            //series2.Values[0] = PieValue3;
            //series3.Values[0] = PieValue4;
            //series4.Values[0] = PieValue5;
            //series2.Values[0] = C;
        }
        #endregion

        #region 柱状图
        [ObservableProperty]
        private SeriesCollection barChartSeries = new();
        public double BarValue1 { get; set; }
        public double BarValue2 { get; set; }
        public double BarValue3 { get; set; }
        public double BarValue4 { get; set; }

        public double BarValue5 { get; set; }

        private void InitializeBarData()
        {
            // 初始化柱状图数据
            var initialValues = new List<double>
            {
                BarValue1,
                BarValue2,
                BarValue3,
                BarValue4,
                BarValue5
            };
            var initialColors = new List<Brush> { Brushes.Purple, Brushes.Red };

            ColumnSeries columnSeries1 =
                new()
                {
                    Title = "产品1当前产量",
                    Values = new ChartValues<double>() { initialValues[0] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue1}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };

            ColumnSeries columnSeries2 =
                new()
                {
                    Title = "产品2当前产量",
                    Values = new ChartValues<double>() { initialValues[1] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue2}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };

            ColumnSeries columnSeries3 =
                new()
                {
                    Title = "产品3当前产量",
                    Values = new ChartValues<double>() { initialValues[2] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue3}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };

            ColumnSeries columnSeries4 =
                new()
                {
                    Title = "产品4当前产量",
                    Values = new ChartValues<double>() { initialValues[3] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue4}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };

            ColumnSeries columnSeries5 =
                new()
                {
                    Title = "产品5当前产量",
                    Values = new ChartValues<double>() { initialValues[4] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue5}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };

            BarChartSeries.Add(columnSeries1);
            BarChartSeries.Add(columnSeries2);
            BarChartSeries.Add(columnSeries3);
            BarChartSeries.Add(columnSeries4);
            BarChartSeries.Add(columnSeries5);
        }

        public void UpdateBarData()
        {
            var series0 = BarChartSeries[0] as ColumnSeries;
            var series1 = BarChartSeries[1] as ColumnSeries;
            var series2 = BarChartSeries[2] as ColumnSeries;
            var series3 = BarChartSeries[3] as ColumnSeries;
            var series4 = BarChartSeries[4] as ColumnSeries;
            series0.Values[0] = BarValue1;
            series1.Values[0] = BarValue2;
            series2.Values[0] = BarValue3;
            series3.Values[0] = BarValue4;
            series4.Values[0] = BarValue5;

            //series0.LabelPoint = (a) =>
            //{
            //    return $"{BarValue1}件";
            //};
            //series1.LabelPoint = (a) =>
            //{
            //    return $"{BarValue2}件";
            //};
            //series2.LabelPoint = (a) =>
            //{
            //    return $"{BarValue3}件";
            //};
            //series3.LabelPoint = (a) =>
            //{
            //    return $"{BarValue4}件";
            //};
            //series4.LabelPoint = (a) =>
            //{
            //    return $"{BarValue5}件";
            //};
        }
        #endregion

        #region 柱状图2
        [ObservableProperty]
        private SeriesCollection barChartSeries1 = new();

        public double BarValue11 { get; set; }
        public double BarValue21 { get; set; }
        public double BarValue31 { get; set; }
        public double BarValue41 { get; set; }

        public double BarValue51 { get; set; }
        private void InitializeBarData1()
        {
            // 初始化柱状图数据
            // 初始化柱状图数据
            var initialValues = new List<double>
            {
                BarValue11,
                BarValue21,
                BarValue31,
                BarValue41,
                BarValue51
            };
            var initialColors = new List<Brush> { Brushes.Purple, Brushes.Red };

            ColumnSeries columnSeries1 =
                new()
                {
                    Title = "产品1计划产量",
                    Values = new ChartValues<double>() { initialValues[0] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue11}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };

            ColumnSeries columnSeries2 =
                new()
                {
                    Title = "产品2计划产量",
                    Values = new ChartValues<double>() { initialValues[1] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue21}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };

            ColumnSeries columnSeries3 =
                new()
                {
                    Title = "产品3当前产量",
                    Values = new ChartValues<double>() { initialValues[2] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue31}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };

            ColumnSeries columnSeries4 =
                new()
                {
                    Title = "产品4当前产量",
                    Values = new ChartValues<double>() { initialValues[3] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue4}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };

            ColumnSeries columnSeries5 =
                new()
                {
                    Title = "产品5计划产量",
                    Values = new ChartValues<double>() { initialValues[4] },
                    StrokeThickness = 1,
                    LabelPoint = (a) =>
                    {
                        return $"{BarValue51}件";
                    },
                    DataLabels = true,
                    FontSize = 14,
                    Foreground = Brushes.White
                };



            BarChartSeries1.Add(columnSeries1);
            BarChartSeries1.Add(columnSeries2);
            BarChartSeries1.Add(columnSeries3);
            BarChartSeries1.Add(columnSeries4);
            BarChartSeries1.Add(columnSeries5);
        }

        public void UpdateBarData1()
        {
            var series0 = BarChartSeries1[0] as ColumnSeries;
            var series1 = BarChartSeries1[1] as ColumnSeries;
            var series2 = BarChartSeries1[2] as ColumnSeries;
            var series3 = BarChartSeries1[3] as ColumnSeries;
            var series4 = BarChartSeries1[4] as ColumnSeries;
            series0.Values[0] = BarValue11;
            series1.Values[0] = BarValue21;
            series2.Values[0] = BarValue31;
            series3.Values[0] = BarValue41;
            series4.Values[0] = BarValue51;

            //series0.LabelPoint = (a) =>
            //{
            //    return $"{BarValue1}件";
            //};
            //series1.LabelPoint = (a) =>
            //{
            //    return $"{BarValue2}件";
            //};
            //series2.LabelPoint = (a) =>
            //{
            //    return $"{BarValue3}件";
            //};
            //series3.LabelPoint = (a) =>
            //{
            //    return $"{BarValue4}件";
            //};
            //series4.LabelPoint = (a) =>
            //{
            //    return $"{BarValue5}件";
            //};
        }
        #endregion

        #region 仪表盘
        //private double gaugeValue;
        public double GaugeValue { get; set; }
        #endregion
    }
}
